package com.qen.zhihulanzhou

import cn.hutool.core.lang.Console
import cn.hutool.core.text.UnicodeUtil
import cn.hutool.json.JSONObject
import cn.hutool.json.JSONUtil
import cn.hutool.poi.excel.ExcelUtil
import it.skrape.fetcher.BrowserFetcher
import it.skrape.fetcher.Method
import it.skrape.fetcher.response
import it.skrape.fetcher.skrape

const val GoUrl = "https://wwa.lanzoux.com/filemoreajax.php?file=3402073"
const val UserAgent =
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
const val LanZhouExcelPath = "D:\\codework\\JavaWorks\\kotlin-skrape\\out\\lanzhou"


fun main() {
    var page = 1
    val ibjtip = 1730634411
    val _hi5ki = "fa654d9b43e9c6b3321b74ce8a244318"
    val list = ArrayList<LanZhou>()
    while (true) {
        var flag = true
        skrape(BrowserFetcher) {
            request {
                url = GoUrl
                userAgent = UserAgent
                body =
                    "lx=2&fid=3402073&uid=1031222&pg=${page}&rep=0&t=${ibjtip}&k=${_hi5ki}&up=1&ls=1&pwd=19v5"
                method = Method.POST
            }
            response {
                Console.log(this.responseStatus)
                Console.log(this.responseBody)
                val json: JSONObject = JSONUtil.parseObj(this.responseBody)
                if (this.responseStatus.code == 200 && json.getStr("info") == "sucess") {

                    val array = json.getJSONArray("text")
                    for (i in 0..array.size - 1) {
                        val obj = array.getJSONObject(i)
                        list.add(
                            LanZhou(
                                id = obj.getStr("id"),
                                name = UnicodeUtil.toString(obj.getStr("name_all")),
                                size = obj.getStr("size")
                            )
                        )
                    }
                } else {
                    flag = false
                }
            }
        }
        page++
        if (flag == false) {
            break
        }
    }

    val writer = ExcelUtil.getWriter("$LanZhouExcelPath/All.xlsx")
    writer.passCurrentRow()
    writer.write(list, true)
    writer.close()
}

data class LanZhou(
    val id: String,
    val name: String,
    val size: String
)

fun initHeader(): MutableMap<String, String> {
    return mutableMapOf(
        "accept" to "application/json, text/javascript, */*",
        "accept-language" to "zh-CN,zh;q=0.9,en;q=0.8",
        "cache-control" to "no-cache",
        "content-type" to "application/x-www-form-urlencoded",
        "pragma" to "no-cache",
        "priority" to "u=1, i",
        "sec-ch-ua" to "\"Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99\"",
        "sec-ch-ua-mobile" to "?0",
        "sec-ch-ua-platform" to "\"macOS\"",
        "sec-fetch-dest" to "empty",
        "sec-fetch-mode" to "cors",
        "sec-fetch-site" to "same-origin",
        "x-requested-with" to "XMLHttpRequest"
    )
}

fun initCookie(): MutableMap<String, String> {
    return mutableMapOf(
        "codelen" to "1",
        "Hm_lvt_fb7e760e987871d56396999d288238a4" to "1730084512",
        "HMACCOUNT" to "8E7727E55235B30C",
        "pc_ad1" to "1",
        "uz_distinctid" to "192d1135d4922b-0fcc89be8dcd1b-1f525636-232800-192d1135d4abd9",
        "STDATA82" to "czst_eid%3D409653338-3821-https%253A%252F%252Fwwa.lanzoux.com%252F%26ntime%3D3821",
        "Hm_lpvt_fb7e760e987871d56396999d288238a4" to "1730084652",
    )
}